package com.zxkj.commonpatrol.repoisity;

import com.allen.library.base.BaseHttpUtil;
import com.allen.library.utils.HttpConnectionUtil;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.SPUtils;
import com.zxkj.commonpatrol.model.post.PostAlarmPhotoInfo;
import com.zxkj.commonpatrol.model.post.PostAlarmVoiceInfo;
import com.zxkj.commonpatrol.model.response.MediaInfoResponse;

import org.json.JSONObject;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class PlayerVioceRepoisity extends BaseRepository {
    public PlayerVioceRepoisity() {
    }









    //下载vioce(指定方法,不可复用)

    public  void   DownVi(Map<Integer, MediaInfoResponse.MediaVoice> maps,MyBaseDataCallBack<Map<Integer, String>> myBaseDataCallBack){

        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<Integer, String> downLoads=new HashMap<>();
                Iterator<Map.Entry<Integer, MediaInfoResponse.MediaVoice>> entries = maps.entrySet().iterator();


                next(entries,downLoads);


                myBaseDataCallBack.onDataSuccessCallBack(downLoads);

            }
        }).start();

    }

    public  void  next(Iterator<Map.Entry<Integer, MediaInfoResponse.MediaVoice>> entries, Map<Integer, String> downLoads){
        if (entries.hasNext()){
            Map.Entry<Integer, MediaInfoResponse.MediaVoice> entry = entries.next();
            PostAlarmVoiceInfo postAlarmVoiceInfo = new PostAlarmVoiceInfo();
            postAlarmVoiceInfo.setVoiceId(entry.getValue().VoiceId);
            int groupId = entry.getValue().getGroupId();
            postAlarmVoiceInfo.setGroupId(groupId);
            postAlarmVoiceInfo.setIndex(entry.getValue().Index);
            Map<Object, Object> objectObjectMap = new BaseHttpUtil<PostAlarmVoiceInfo>().MyconvertVo2Params(postAlarmVoiceInfo);
            JSONObject object = new JSONObject(objectObjectMap);
            String fileNames = null;
            try {
                fileNames = com.allen.library.utils.TimeTool.format(System.currentTimeMillis(), com.allen.library.utils.TimeTool.format3) +
                        entry.getValue().VoiceId+entry.getValue().Index+entry.getValue().Index+ ".amr";
            } catch (ParseException e) {
                e.printStackTrace();
            }
            HttpConnectionUtil.downloadFile(ItrackConstant.BASE_URL + "/api/Logs/DownloadAlarmLogVoice", ItrackConstant.DownPicture, SPUtils.get(ItrackConstant.TOKEN, ""), SPUtils.get(ItrackConstant.LOGINID, ""), object.toString(),fileNames, new HttpConnectionUtil.MyBaseDataCallBack<String>() {
                @Override
                public void onDataSuccessCallBack(String s) {
                    downLoads.put(entry.getKey(),s);


                        next(entries,downLoads);


                }

                @Override
                public void onDataFailCallBack(String s, String t1) {

                        next(entries,downLoads);

                }
            });

        }
    }










}
